package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.DriRandomBonusDao;
import benny_driving_ser.dao.impl.DriRandomBonusImpl;
import benny_driving_ser.dao.impl.FormDaoImpl;
import benny_driving_ser.entity.Form;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.SendSMSUtil;
import benny_driving_ser.util.SessionUtil;

public class FormServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1455212118516226544L;

	/**
	 * Constructor of the object.
	 */
	public FormServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if ("dri-qdc".equals(action.trim())) {
			// String driid;
			// HttpSession session = request.getSession();
			// String driId = (String)session.getAttribute("driId");
			// if (null == (driid = request.getParameter("driid")))
			// return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				List<Form> forms = FormDaoImpl.getInstance().queryAllForms(
						Integer.parseInt(sad.getValue()));
				if (forms != null) {
					JSONObject jo = new JSONObject();
					JSONArray ja = new JSONArray();
					try {
						for (Form form : forms) {
							JSONObject mjo = new JSONObject();
							mjo.put("formid", form.getFormid());
							mjo.put("username", form.getName());
							mjo.put("time", form.getSertime());
							mjo.put("startpos", form.getStartpos());
							mjo.put("despos", form.getDespos());
							mjo.put("excost", form.getExcost());
							mjo.put("exmil", form.getExmil());
							mjo.put("bz", form.getBz());
							ja.put(mjo);
						}
						jo.put("resforms", ja);
						out.print(jo.toString());
					} catch (Exception e) {
						out.print("{'resforms':[]}");
						e.printStackTrace();
					}
				} else {
					out.print("{'resforms':[]}");
				}
			} else
				out.print(-1);
		}
		if ("dri-qd".equals(action.trim())) {
			String formid;
			DriRandomBonusDao dao = new DriRandomBonusImpl();
			
			if (null == (formid = request.getParameter("formid")))
				return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				
				int res = FormDaoImpl.getInstance().selectForm(
						Integer.parseInt(formid.trim()),
						Integer.parseInt(sad.getValue()));
				
				//res==5是司机账号被冻结
				System.out.println("res"+res);
				
				out.print(res);
			} else {
				out.print(-1);
			}
		}
		if ("dri-yjddcx".equals(action.trim())) {
			// String driid;
			// if (null == (driid = request.getParameter("driid")))
			// return;
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				Form form = FormDaoImpl.getInstance().querySelectedForms(
						Integer.parseInt(sad.getValue()));
				if (form != null) {
					JSONObject json = new JSONObject();
					try {
						json.put("formid", form.getFormid() + "");
						json.put("sertime", form.getSertime() + "");
						json.put("username", form.getName() + "");
						json.put("usertel", form.getTel() + "");
						json.put("driname", form.getDriname() + "");
						json.put("dritel", form.getDritel() + "");
						json.put("drinum", form.getDrinum() + "");
						json.put("startpos", form.getStartpos() + "");
						json.put("despos", form.getDespos() + "");
						json.put("exmil", form.getExmil() + "");
						json.put("excost", form.getExcost() + "");
						json.put("formstate", form.getFormstate() + "");
						json.put("cardstate", form.getCardstate() + "");
						json.put("cardvalue", form.getCardvalue() + "");
						json.put("bz", form.getBz()+"");
						json.put("bonus", form.getBonus()+"");
						json.put("orderclass", form.getOrderclass()+"");
						out.print(json.toString());
					} catch (JSONException e) {
						out.print("{'formstate':-1}");
						e.printStackTrace();
					}
				}
			} else
				out.print(-1);
		}
		if ("dri-lsddcx".equals(action.trim())) {

		}
		if ("user-ddsc".equals(action.trim())) {
			try {
				String userid, startpos, despos, excost, exmil, sertime, username, usertel;
				if (null == (userid = request.getParameter("userid"))
						|| null == (startpos = request.getParameter("startpos"))
						|| null == (despos = request.getParameter("despos"))
						|| null == (excost = request.getParameter("excost"))
						|| null == (exmil = request.getParameter("exmil"))
						|| null == (sertime = request.getParameter("sertime"))
						|| null == (username = request.getParameter("username"))
						|| null == (usertel = request.getParameter("usertel")))
					return;
				String res = FormDaoImpl.getInstance().createForm(
						Integer.parseInt(userid.trim()), startpos, despos,
						excost, exmil, Long.parseLong(sertime.trim()),
						username, usertel);
				out.print(res);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				out.print(2);
			}
		}
		if ("user-dhddsc".equals(action.trim())) {
			String driid, userid;
			try {
				if (null == (driid = request.getParameter("driid"))
						|| null == (userid = request.getParameter("userid")))
					return;
				int res = FormDaoImpl.getInstance().createFormByCall(
						Integer.parseInt(userid.trim()),
						Integer.parseInt(driid.trim()));
				out.print(res);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				out.print(3);
			}
		}
		if ("user-yyddcx".equals(action.trim())) {

		}
		if ("user-lsddcx".equals(action.trim())) {

		}
		if ("user-bcyydd".equals(action.trim())) {

		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	
	private String getRemortIP(HttpServletRequest request) {

		if (request.getHeader("x-forwarded-for") == null) {
			
		return request.getRemoteAddr();
		}
		return null;
	
	}
}
